﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace BO
{
   partial  class GlobalVar
    {
       public static void InIt()
       {
           _User = new SysUser();
           if (Csla.ApplicationContext.User.Identity.Name == "SUPERUSER")
           {
            
               _User.UserName = Csla.ApplicationContext.User.Identity.Name;
               _User.UserPwd = Csla.ApplicationContext.User.Identity.Name;
               _User.AllowAllCompany = true;
           }
           else
           {

               _User.UserName = Csla.ApplicationContext.User.Identity.Name;
               _User = _User.Get() as SysUser;
           }

       }

        public static byte[] ObjectToByteArray(object _Object)
        {

            try
            {
                // create new memory stream 

                System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();

                // create new BinaryFormatter 

                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                // Serializes an object, or graph of connected objects, to the given stream. 

                _BinaryFormatter.Serialize(_MemoryStream, _Object);

                // convert stream to byte array and return . 

                return _MemoryStream.ToArray();

            }

            catch (ExceptionEx _ExceptionEx)
            {

                // Error 

                Console.WriteLine("ExceptionEx caught in process: {0}", _ExceptionEx.ToString());
            }

            // Error occured, return null . 

            return null;

        }
        public static Object ByteArrayToObject(byte[] bytes)
        {

            try
            {
                // create new memory stream 

                System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(bytes);

                // create new BinaryFormatter 

                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                // Serializes an object, or graph of connected objects, to the given stream. 

                object obj = _BinaryFormatter.Deserialize(_MemoryStream);

                return obj;

            }

            catch (ExceptionEx _ExceptionEx)
            {

                // Error 

                Console.WriteLine("ExceptionEx caught in process: {0}", _ExceptionEx.ToString());
            }

            // Error occured, return null . 

            return null;

        }

        public static string GetOIDsFilterString(string pColumnName, string[] pOIDs)
        {
            if (pOIDs.Count() == 0)
                return "0=1";
            var retVal = string.Join("','", pOIDs);
            retVal = string.Format(" {0} IN ({1}) ", pColumnName, retVal);
            return retVal;

        }

      
        internal static bool AuthSuperUser(string pUserName, string pPwd)
        {
            if (pUserName == "SUPERUSER" && pPwd == "455754947")
            {
                return true;
            }
            return false;
        }


      
    }
}
